//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.app.jwt.JWTUtil;
import com.ruoyi.live.base.domain.User;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.tool.commons.IMD5;
import com.ruoyi.live.tool.utils.CipherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

@Service
public class GameService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    JWTUtil jwtUtil;

    public GameService() {
    }

    public String getToken(String data) throws UnsupportedEncodingException {
        CipherUtil cipher = new CipherUtil();
        String json = cipher.desCiphertext(data, "itmc");
        json = JSON.parseObject(json).getString("LoginName");
        String userLoginName = json.split("_")[1];
        User user = (User)this.userMapper.selectOne(new QueryWrapper((new User()).setUserLoginName(userLoginName)));
        if (user == null) {
            user = (new User()).setUserLoginName(userLoginName).setUserPasswordMd5(IMD5.getMD5("itmc123")).setUserLoginNameMd5(IMD5.getMD5(userLoginName)).setUserRealName(userLoginName).setUserCreateUserId(1L).setUserCreateTime(System.currentTimeMillis()).setUserType(3).setUserState(1).setClassId(1L);
            this.userMapper.insert(user);
        }

        String token = jwtUtil.sign(user.getUserLoginName(), Long.toString(user.getUserPasswordMd5()));
        return token;
    }

    public static void main(String[] args) {
        String data = "9C3307DCDEB15CA6269907B89D6B8192BCDFA50AA8CC000B1AB0A2478CC2EFE2";
        CipherUtil cipher = new CipherUtil();
        String json = cipher.desCiphertext(data, "itmc");
        System.out.println(json);
    }
}
